/**
 * Created by George on 2018/1/28.
 * mainCtrl
 */
// 将本控制器函数作为结果返回给router.js
function mainCtrl($scope) {
    $scope.menuList = [
        {name: "user", href: "#/user", class: "item-select"},
        {name: "activity", href: "#/activity", class: ""},
        {name: "dues", href: "#/dues", class: ""},
        {name: "note", href: "#/note", class: ""}
    ];
    $scope.value = "this is main ctrl.";
    $scope.welcome_info = "this is main ctrl.";


    function init() {
        var url = window.location.href;
        var routeValue = url.substr(url.indexOf('#'));
        initRoute(routeValue);
    }

    function initRoute(routeValue) {
        var itemSelect = false;
        for (var index = 0; index < $scope.menuList.length; index++) {
            if (routeValue.indexOf($scope.menuList[index].href) > -1
                || routeValue === $scope.menuList[index].name) {
                itemSelect = true;
                $scope.menuList[index].class = 'item-select';
            } else {
                $scope.menuList[index].class = '';
            }
        }
        if (!itemSelect && $scope.menuList.length > 0) {
            $scope.menuList[0].class = 'item-select';
        }
    }

    $scope.itemClick = function (data) {
        initRoute(data.href)
    };

    $scope.$on('updateRoute', function (event, data) {
        initRoute(data);
    });

    //app start
    init();
}

export default mainCtrl;
